/**
 * Created by WYH on 17/4/18.
 */
angular.module('tribalc').service( 'CompanyModel', ['Check', 'Api' ,function (Check, Api) {

    var CompanyModel = function (object) {

        var self = this;

        init();

        function init() {
            self.id = object ? object.id : null;
            self.name = object ? object.name : null;
            self.password = object ? object.password : null;
            self.province = object ? object.province : null;
            self.city = object ? object.city : null;
            self.district = object ? object.district : null;
            self.address = object ? object.address: null;
            self.desc = object ? object.desc : null;
            self.companyName = object ? object.companyName : null;
            self.companyIndustry = object ? object.companyIndustry : null;
            self.companyStaffNum = object ? object.companyStaffNum : null;
            self.companyNature = object ? object.companyNature : null;
            self.linkman = object ? object.linkman : null;
            self.department = object ? object.department : null;
            self.phone = object ? object.phone : null;
            self.mobilePhone = object ? object.mobilePhone : null;
            self.verificationCode = object ? object.verificationCode : null;
            self.email = object ? object.email : null;
            self.licenseNum = object ? object.licenseNum : null;
            self.pictures = object ? getReallyPicturePaths(object.pictures) : null;
            self.legalPersonName = object ? object.legalPersonName : null;
            self.legalPersonIdCardNo = object ? object.legalPersonIdCardNo : null;
            self.legalPersonPhone = object ? object.legalPersonPhone : null;
            self.authenticationStatus = object ? object.authenticationStatus : null;
            self.propertyNature = object ? object.propertyNature : null;
            self.communityId = object ? object.communityId : null;
            self.communityName = object ? object.communityName : null;
            self.lockAuthorized = object ? object.lockAuthorized : null;

            self.licensePicture = object ? CompanyModel.formatReallyPicturePath(object.licensePicture) : null;
            self.logo = object ? CompanyModel.formatReallyPicturePath(object.logo) : null;
            self.underCover = object ? CompanyModel.formatReallyPicturePath(object.underCover) : null;

            self.community = object ? { id: object.communityId, name: object.communityName } : null;

            self.repassword = null;
        }



    };


    function getReallyPicturePaths(pictures) {
        if (!pictures) return [];
        pictures.forEach(function (path, index) {
            pictures[index] = CompanyModel.formatReallyPicturePath(path);
        });
        return pictures;
    }
    
    function getOssPicturePaths(pictures) {
        if (!pictures) return [];
        pictures.forEach(function (path, index) {
            pictures[index] = CompanyModel.formatOssPicturePath(path);
        });
        return pictures;
    }

    CompanyModel.prototype.getObject = function () {
        return {
            name: this.name,
            propertyNature: this.propertyNature,
            communityName: this.community.name,
            communityId: this.community.id,
            companyName: this.companyName,
            province: this.province,
            city: this.city,
            desc: this.desc,
            district: this.district,
            address: this.address,
            companyStaffNum: this.companyStaffNum,
            companyIndustry: this.companyIndustry,
            companyNature: this.companyNature,
            logo: CompanyModel.formatOssPicturePath(this.logo),
            underCover: CompanyModel.formatOssPicturePath(this.underCover),
            licensePicture: CompanyModel.formatOssPicturePath(this.licensePicture),
            linkman: this.linkman,
            department: this.department,
            phone: this.phone,
            email: this.email,
            pictures: getOssPicturePaths(this.pictures),
            legalPersonName: this.legalPersonName,
            legalPersonIdCardNo: this.legalPersonIdCardNo,
            legalPersonPhone: this.legalPersonPhone,
            licenseNum: this.licenseNum
        }
    };


    CompanyModel.formatOssPicturePath = function (path) {
        if (path && path.indexOf(Api.ALIYUN_URL) !== -1) {
            path = 'oss://' + path.split(Api.ALIYUN_URL)[1];
        }
        return path;
    };

    CompanyModel.formatReallyPicturePath = function (path) {
        var oss = 'oss://';
        if (path && path.indexOf(oss) !== -1) {
            path = Api.ALIYUN_URL + path.split(oss)[1];
        }
        return path;
    };

    CompanyModel.prototype.isValidRegister = function () {
        return Check.company(this);
    };

    CompanyModel.prototype.isValidDetail = function () {

    };



    return CompanyModel;




}]);